package process

import (
	"bufio"
	utils "chatOnline/client/uitls"
	"chatOnline/common/message"
	"encoding/json"
	"fmt"
	"io"
	"os"
)

type Action struct {
}

func (this *Action) FileUpLoad() (err error) {
	file, err := os.Open("E:\\ProgramData\\GoWorkSpace\\src\\go_code\\chatOnline\\client\\process\\test.txt")

	defer file.Close()

	if err != nil {
		fmt.Println("open file err=", err.Error())
		return
	}
	mes := message.FileLoadMes{
		FileName: "test.txt",
	}
	reader := bufio.NewReader(file)
	buf := make([]byte, 4096)
	for {
		n, err1 := reader.Read(buf) // 读到一个换行就结束
		if err1 == io.EOF {         //io.EOF 表示文件的结尾
			break
		}
		mes.FileData = buf
		mes.FileLen = n
		data, err1 := json.Marshal(mes)
		if err1 != nil {
			fmt.Println(" json marshal err=", err1.Error())
			err = err1
			return
		}

		mes2 := message.Message{
			Type: message.FileLoadType,
			Data: string(data),
		}

		tf := utils.Transfer{
			Conn: CurUser.Conn,
		}
		data, err1 = json.Marshal(mes2)
		err1 = tf.WritePkg(data)
		if err1 != nil {
			fmt.Println(" file load writerPkg err=", err1.Error())
			err = err1
			return
		}
	}

	return
}
